from datetime import datetime
from typing import List

from sqlalchemy.orm import Session

from app.models.personalized_vocabulary import PersonalizedVocabulary
from app.schemas.personalized_vocabulary import PersonalizedVocabularyCreate, PersonalizedVocabularyUpdate
from app.crud.base import CRUDBase

# 个性化词汇的 CRUD 操作类
class CRUDPersonalizedVocabulary(CRUDBase[PersonalizedVocabulary, PersonalizedVocabularyCreate, PersonalizedVocabularyUpdate]):
    def get_by_category_name(self, db: Session, category_name: str) -> List[PersonalizedVocabulary]:
        """
        根据分类名称查询个性化词汇。
        """
        return db.query(PersonalizedVocabulary).filter(PersonalizedVocabulary.category_name == category_name).all()

    def get_by_date_range(self, db: Session, start_date: datetime, end_date: datetime) -> List[PersonalizedVocabulary]:
        """
        根据日期范围查询个性化词汇。
        """
        return db.query(PersonalizedVocabulary).filter(PersonalizedVocabulary.created_at >= start_date,
                                                       PersonalizedVocabulary.created_at <= end_date).all()

    def get_unique_categories(self, db: Session) -> List[str]:
        """
        获取不重复的分类名称列表。
        """
        result = db.query(PersonalizedVocabulary.category_name).distinct().all()
        return [category[0] for category in result]

# 实例化 CRUD 对象，以便在路由（route）中使用
personalized_vocabulary = CRUDPersonalizedVocabulary(PersonalizedVocabulary)
